package org.suburban.servlet.schedule;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

/**
 * Class to "parse" 'full-day' HTML schedule response  
 * @author Alexey Palamarchuk (slonnik@gmail.com)
 */
final class FullDayScheduleCracker extends ScheduleCracker {

	/**
	 * ctor
	 * {@inheritDoc}
	 */
	FullDayScheduleCracker(StringBuilder out, String header, boolean isXml)
	{
		super(out, header, isXml);
	}
	
	@Override
	public void startElement(String arg0, String arg1, String arg2,
			Attributes arg3) throws SAXException {
		/**
		 * try to locate <ul class="b-holster b-schedule-electric-train">
		 */
		if(arg2.compareToIgnoreCase("ul") == 0)
		{
			String className = arg3.getValue("class");
			if((className != null) && 
					(className.compareToIgnoreCase("b-holster b-schedule-electric-train") == 0))
			{
				//we start parsing schedule data
				parsingTable = true;
				return;
			}
		}
		if(parsingTable == false)
		{
			//we are not interested in handling this element any more
			return;
		}
		/**
		 * start processing 1 record in the table
		 */
		if(arg2.compareToIgnoreCase("li") == 0)
		{
			item.clear();
		}
		if(arg2.compareToIgnoreCase("a") == 0)
		{
			/**
			 * trying to get time like
			 * <a href="/thread/6804A_2000002_af?departure=2010-01-22&station_from=2000002&station_to=9600681">04:45</a>
			 */
			parsingTime = true;
			return;
		}
		if(arg2.compareToIgnoreCase("em") == 0)
		{
			/**
			 * trying to get period like
			 * <em class="period">bla-bla</em>
			 */
			parsingPeriod = true;
			return;
		}
		
	}
	@Override
	public void characters(char[] arg0, int arg1, int arg2) throws SAXException {
		if(parsingTime == true)
		{
			item.departure = new String(arg0, arg1, arg2).trim();
			return;
		}
		if(parsingPeriod == true)
		{
			item.period = new String(arg0, arg1, arg2).trim();
			return;
		}
		if(parsingArrivalTime == true)
		{
			item.arrival = new String(arg0, arg1, arg2).trim();
			return;
		}
	}
	@Override
	public void endElement(String arg0, String arg1, String arg2)
			throws SAXException {
		/**
		 * try to locate </ul>
		 */
		if(arg2.compareToIgnoreCase("ul") == 0)
		{
			//we finished parsing schedule data
			parsingTable = false;
			return;
		}
		if(parsingTable == false)
		{
			//we are not interested in handling this element any more
			return;
		}
		/**
		 * finished processing 1 record in the table
		 */
		if(arg2.compareToIgnoreCase("li") == 0)
		{
			if(isXml)
			{
				out.append("<tr>");
				out.append("<td nowrap=\"nowrap\">").append(item.departure).append("</td>");
				out.append("<td nowrap=\"nowrap\">").append(" > ").append("</td>");
				out.append("<td nowrap=\"nowrap\">").append(item.arrival).append("</td>");
				out.append("<td>").append(item.period).append("</td>");
				out.append("</tr>");
			}
			else
			{
				out.append(item.toString()).append('\n');
			}
		}
		/**
		 * stop parsing time
		 */
		if(arg2.compareToIgnoreCase("a") == 0)
		{
			parsingTime = false;
			return;
		}
		if(arg2.compareToIgnoreCase("em") == 0)
		{
			parsingPeriod = false;
			return;
		}
		/**
		 * <img> element is ended, and we can start parsing arrival time
		 */
		if(arg2.compareToIgnoreCase("img") == 0)
		{
			parsingArrivalTime = true;
			return;
		}
		/**
		 * <span> element is ended, and we stop parsing arrival time
		 */
		if(arg2.compareToIgnoreCase("span") == 0)
		{
			parsingArrivalTime = false;
			return;
		}
	}
}
